
# 执行过程
def demo_method():
    print('Starting......')
    while True:
        res = yield 4
        print("res: ", res)

def demo_1():
    g = demo_method()
    print("第一次调用执行结果:")
    print(next(g))
    print("*" * 100)
    print("第二次调用执行结果:")
    print(next(g))
    print("*" * 100)
    print("第三次调用执行结果:")
    print(next(g))
    print("*" * 100)

def demo_2():
    g = demo_method()
    print("第一次调用执行结果:")
    print(next(g))
    print("*" * 100)
    print("第二次调用执行结果(传入参数):")
    print(g.send(10))
    print("*" * 100)
    print("第三次调用执行结果:")
    print(next(g))
    print("*" * 100)

# 如果使用单纯for循环，需要大量内存
def demo_method_4(n):
    for n in range(10):
        a = n
        print(a)
    print("*" * 100)

# 使用yield后，底层原理为中断的原理，保存栈帧，加载栈帧，每次执行结束内存释放，执行的时候占用一点内存
def demo_method_3(n):
    print("starting....")
    while n < 10:
        n = n + 1
        yield n

def demo_3():
    for n in demo_method_3(0):
        print(n)


if __name__ == '__main__':
    # demo_1()
    # demo_2()
    demo_3()